package com.fantasyland.backend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/health")
@CrossOrigin(
    origins = {"http://localhost:5173", "http://127.0.0.1:5173"}, 
    methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS},
    allowedHeaders = "*",
    allowCredentials = "true"
)
public class HealthController {
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    /**
     * 基本健康检查
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> health() {
        Map<String, Object> response = new HashMap<>();
        response.put("status", "UP");
        response.put("service", "Forum Backend");
        response.put("timestamp", System.currentTimeMillis());
        return ResponseEntity.ok(response);
    }
    
    /**
     * 检查远程Redis连接状态
     */
    @GetMapping("/redis")
    public ResponseEntity<Map<String, Object>> checkRedisHealth() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 尝试执行Redis ping命令
            String pong = redisTemplate.getConnectionFactory().getConnection().ping();
            
            response.put("status", "UP");
            response.put("redis", "Connected");
            response.put("ping", pong);
            response.put("server", "43.142.89.195:6379");
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            response.put("status", "DOWN");
            response.put("redis", "Disconnected");
            response.put("server", "43.142.89.195:6379");
            response.put("error", e.getMessage());
            response.put("errorType", e.getClass().getSimpleName());
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.status(503).body(response);
        }
    }
    
    /**
     * 测试远程Redis读写操作
     */
    @GetMapping("/redis/test")
    public ResponseEntity<Map<String, Object>> testRedisOperations() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String testKey = "health_test_" + System.currentTimeMillis();
            String testValue = "test_value_" + System.currentTimeMillis();
            
            // 写入测试
            redisTemplate.opsForValue().set(testKey, testValue);
            
            // 读取测试
            String retrievedValue = (String) redisTemplate.opsForValue().get(testKey);
            
            // 删除测试数据
            redisTemplate.delete(testKey);
            
            response.put("status", "UP");
            response.put("server", "43.142.89.195:6379");
            response.put("write", "SUCCESS");
            response.put("read", "SUCCESS");
            response.put("delete", "SUCCESS");
            response.put("testValue", testValue);
            response.put("retrievedValue", retrievedValue);
            response.put("match", testValue.equals(retrievedValue));
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(response);
            
        } catch (Exception e) {
            response.put("status", "DOWN");
            response.put("server", "43.142.89.195:6379");
            response.put("error", e.getMessage());
            response.put("errorType", e.getClass().getSimpleName());
            response.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.status(503).body(response);
        }
    }
}